package com.education.basic.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.education.basic.ao.impl.StudentAoImpl;
import com.education.basic.ao.impl.TeacherAoImpl;
import com.education.basic.entity.Department;
import com.education.basic.entity.Student;
import com.education.basic.resource.Major;
import com.education.basic.service.StudentService;
import com.education.basic.utils.CodeProvider;
import com.yunshangwoniu.core.baseAction.BaseAction;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.MD5Utils;

@Controller
@Scope("prototype")
@Namespace("/student")
@ParentPackage(value="struts-default")
public class StudentAction extends BaseAction
{
	private static final long serialVersionUID = 1L;
	
	@Resource
	private StudentAoImpl studentAo;
	@Resource
	private StudentService studentService;
	private List<Major> majors;
	private List<Department> departments;
	private Student student;
	private Page<Student> page;
	private Integer pageNo;	
	private String name;
	private long id;
	private String ids;
	@Resource
	private TeacherAoImpl teacherAo;
	
	@Action(value="list",results={@Result(name="success",location="list.jsp")})
	public String list()
	{
		pageNo = pageNo==null?1:pageNo;
		page = studentAo.page(pageNo, 10, null, getRequest(), Student.class);
		return SUCCESS;
	}
	
	@Action(value="search",results={@Result(name="success",location="list.jsp")})
	public String search()
	{
		pageNo = pageNo==null?1:pageNo;
		page = studentAo.findByName(name, getRequest(), Student.class);
		return SUCCESS;
	}
	
	@Action(value="add",results={@Result(name="success",location="add.jsp")})
	public String add()
	{
		majors = Major.queryMajor();
		departments = teacherAo.getAllDepartment();
		return SUCCESS;
	}

	@Action(value="new",results={@Result(name="success",location="../inc/message.jsp")})
	public String newStudent()
	{
		student.setStuNum(CodeProvider.getStudentNo(student));
		student.setPassword(MD5Utils.KL(MD5Utils.MD5(student.getPassword())));
		studentService.newStudent(student);
		setCallback("/student/list.htm");
		return SUCCESS;
	}

	@Action(value="delete")
	public void delete() throws IOException
	{
		studentAo.remove(id, Student.class);
		PrintWriter pw = getResponse().getWriter();
		pw.print("success");
		pw.flush();
		pw.close();
	}
	@Action(value="batchDelete")
	public void batchDelete() throws IOException
	{
		
		String[] arr = ids.split("\\,");
		List<Long> ids2 = new ArrayList<Long>();
		for(String str :arr)
		{
			ids2.add(Long.parseLong(str));
		}
		studentAo.batchDelete(ids2, Student.class);
		PrintWriter pw = getResponse().getWriter();
		pw.print("success");
		pw.flush();
		pw.close();
	}
	
	@Action(value="edit",results={@Result(name="success",location="edit.jsp")})
	public String edit()
	{
		majors = Major.queryMajor();
		departments = teacherAo.getAllDepartment();
		student=studentAo.findById(id, Student.class);
		return "success";
	}
	
	@Action(value="modify",results={@Result(name="success",location="../inc/message.jsp")})
	public String modify()
	{
		studentAo.update(student);
		setCallback("/student/list.htm?pageNo"+pageNo);
		return SUCCESS;
	}

	public StudentAoImpl getStudentAo()
	{
		return studentAo;
	}

	public void setStudentAo(StudentAoImpl studentAo)
	{
		this.studentAo = studentAo;
	}

	public List<Major> getMajors()
	{
		return majors;
	}

	public void setMajors(List<Major> majors)
	{
		this.majors = majors;
	}

	public List<Department> getDepartments()
	{
		return departments;
	}

	public void setDepartments(List<Department> departments)
	{
		this.departments = departments;
	}

	public Student getStudent()
	{
		return student;
	}

	public void setStudent(Student student)
	{
		this.student = student;
	}

	public Page<Student> getPage()
	{
		return page;
	}

	public void setPage(Page<Student> page)
	{
		this.page = page;
	}

	public Integer getPageNo()
	{
		return pageNo;
	}

	public void setPageNo(Integer pageNo)
	{
		this.pageNo = pageNo;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public String getIds()
	{
		return ids;
	}

	public void setIds(String ids)
	{
		this.ids = ids;
	}

	public TeacherAoImpl getTeacherAo()
	{
		return teacherAo;
	}

	public void setTeacherAo(TeacherAoImpl teacherAo)
	{
		this.teacherAo = teacherAo;
	}

	public static long getSerialversionuid()
	{
		return serialVersionUID;
	}
	
}
